<?php

namespace app\modules\client\controllers;


use Yii;
use yii\web\Controller;


class InstallController extends Controller{

    public $enableCsrfValidation = false;

    public function actionIndex(){
        return $this->renderPartial("index");
    }

    public function actionTest(){
        echo "modules test";
    }

    public function actionInstall(){

        $db = Yii::$app->db;

        //请求表
        $exist = true;
        try{
            $count = $db->createCommand("SELECT COUNT(*) FROM `pre_request`")->queryScalar();
        }catch(\Exception $e){
            echo "表不存在，开始创建";
            //echo $e->getMessage();
            $exist = false;
        }

        if(!($count >0) || !$exist){

            $db->createCommand("DROP TABLE IF EXISTS `pre_request`")->execute();

            $sql = "CREATE TABLE `pre_request` (
  `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '文章ID',
  `cid` INT(11) NOT NULL DEFAULT '0' COMMENT '分类ID',
  `name` VARCHAR(300) NOT NULL DEFAULT '' COMMENT '标题',
  `domain` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '域名，例如：http://www.ooe7.com',
  `uri` VARCHAR(80) NOT NULL DEFAULT '' COMMENT 'uri',
  `method` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '请求方式：GET、POST',
  `status` TINYINT(1) NOT NULL DEFAULT '1' COMMENT '状态：1正常',
  PRIMARY KEY (`id`),
  KEY `cid` (`cid`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='请求表';";

            $db->createCommand($sql)->execute();

            $data = [
                [0, '客户端模式获取TOKEN', '', '/oauth2/token','POST'],
                [0, '授权码模式获取CODE', '', '/oauth2/authorize-code','POST'],
            ];

            $db->createCommand()->batchInsert('pre_request',['cid','name','domain','uri','method'], $data)->execute();

        }



        //参数表
        $exist = true;
        try{
            $count = $db->createCommand("SELECT COUNT(*) FROM `pre_request_param`")->queryScalar();
        }catch(\Exception $e){
            echo "参数表不存在，开始创建";
            //echo $e->getMessage();
            $exist = false;
        }

        if(!($count >0) || !$exist){

            $db->createCommand("DROP TABLE IF EXISTS `pre_request_param`")->execute();

            $sql = "DROP TABLE IF EXISTS `pre_request_param`;
CREATE TABLE `pre_request_param` (
  `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '参数ID',
  `rid` INT(11) NOT NULL DEFAULT '0' COMMENT '请求ID',
  `name` VARCHAR(300) NOT NULL DEFAULT '' COMMENT '标题',
  `necessary` TINYINT(100) NOT NULL DEFAULT 0 COMMENT '是否必填：1是，2否',
  `type` VARCHAR(80) NOT NULL DEFAULT '' COMMENT '类型：string,int,enum等',
  `default` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '默认值',
  `intro` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '参数说明',
  `status` TINYINT(1) NOT NULL DEFAULT '1' COMMENT '状态：-2删除，-1草稿，0正常',
  PRIMARY KEY (`id`),
  KEY `rid` (`rid`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='参数表';";

            $db->createCommand($sql)->execute();

            $data = [
                [1, 'grant_type', 1, 'string','client_credentials',''],
                [1, 'client_id', 1, 'string','testclient',''],
                [1, 'client_secret', 1, 'string','testpass',''],
            ];

            $db->createCommand()->batchInsert('pre_request_param',['rid','name','necessary','type','default','intro'], $data)->execute();

        }
    }

}
